package com.jeff.learn.linkedlist;

import java.util.Iterator;

/**
 * 链表测试类
 *
 * @author yaojianfeng
 */
public class LinkListTest {
    public static void main(String[] args) {
//        testSinglyLinkedList();
        testDoublyLinkedList();
    }

    public static void testSinglyLinkedList() {
        SinglySentinelLinkedList list = new SinglySentinelLinkedList();
        for (int i = 0; i < 3; i++) {
            list.addTail(i);
        }
        for (int i = 0; i < 3; i++) {
            list.addHead(i);
        }

        list.foreach((node) -> {
            int value = node.getValue();
            System.out.print(value + "=>");
        });
        System.out.println("==========");
        for (Node node : list) {
            int value = node.getValue();
            System.out.print(value + "=>");
        }
        System.out.println("==========");
        Iterator<Node> iterator = list.iterator();
        iterator.forEachRemaining(node -> System.out.print(node.getValue() + "=>"));
        System.out.println("==========");
        list.forEach(node -> System.out.print(node.getValue() + "=>"));
        System.out.println("==========");

        System.out.println("查找到:" + list.findNode(-1));
        System.out.println("查找到:" + list.findNode(0));
        System.out.println("查找到:" + list.findNode(1));
        System.out.println("查找到:" + list.findNode(2));
        System.out.println("查找到:" + list.findNode(3));


        System.out.println("删除掉:" + list.removeNode(1));
        System.out.println("删除掉:" + list.removeNode(0));
        list.forEach(node -> System.out.print(node.getValue() + "=>"));
        System.out.println("==========");
        System.out.println("插入节点" + list.insertNode(0, new Node(100)));
        System.out.println("插入节点" + list.insertNode(1, new Node(200)));
        System.out.println("插入节点" + list.insertNode(2, new Node(300)));
        System.out.println("插入节点" + list.insertNode(4, new Node(400)));
        System.out.println("插入节点" + list.insertNode(5, new Node(500)));
        list.forEach(node -> System.out.print(node.getValue() + "=>"));
        System.out.println("==========");
    }

    public static void testDoublyLinkedList() {
        DoublySentinelLinkedList list = new DoublySentinelLinkedList();

//        for (int i = 0; i < 3; i++) {
//            list.addTail(i);
//        }
        for (int i = 0; i < 3; i++) {
            list.addHead(i);
        }
        list.forEach(node -> System.out.print(node.getValue() + "=>"));
        System.out.println("==========");
        Node node = list.findNode(2);
        System.out.println("查找到:" + node);
        Node head = list.removeHead();
        System.out.println("删除掉:" + head);
        list.forEach(node1 -> System.out.print(node1.getValue() + "=>"));
        System.out.println("==========");
        Node tail = list.removeTail();
        System.out.println("删除掉:" + tail);
        list.forEach(node1 -> System.out.print(node1.getValue() + "=>"));
        System.out.println("==========");
    }
}
